/**
 * Report 1
 */

var cnTargets = new Connection(contextPath + "/Targets", "GET", "DATAROW", "ERROR");
var cnReport1 = new Connection(contextPath + "/Report1", "GET", "DATAROW", "ERROR");



var dailyReportsData;
var targetOfTheDay = new Array();

var dates = new Array();
var green = new Array();
var blue = new Array();
var red = new Array();

var subporoject_b = new Array();
var subporoject_d = new Array();
var subporoject_e = new Array();

var totalEntries = new Array();

var utilList = new Array();
var utilization = new Array();

var absendsList = new Array();
var greenTargets = new Array();
var redTargets = new Array();
var blueTargets = new Array();
var topGreen = new Array();

var gammaRates = new Array();

// -------------------------------------------------------------------------------------------------------------------------------------------

function Utilization (date, number){
    this.date = date;
    this.number = number;

    return this;
}

Utilization.prototype.getnumber = function () {
    return (this.number / 100) + " %";
}

// -------------------------------------------------------------------------------------------------------------------------------------------

function getLastMonthAverageOfDailyReport() {
	cnReport1.setMethod("GET");
	cnReport1.SetFunctionName("a");
	cnReport1.setArguments("2");
    var results = cnReport1.callNoXML();
    
    //alert(escape("Καλημέρα"));
    
    //alert(unescape(results));

    var dailyAverageReportsData = eval("(function(){return " + unescape(results) + ";})()");
    
    dates.push(dailyAverageReportsData["workingdate"]);

    green.push(dailyAverageReportsData["green"]);
    blue.push(dailyAverageReportsData["blue"]);
    red.push(dailyAverageReportsData["red"]);

    subporoject_b.push(dailyAverageReportsData["totalEntriesForSubprojectB"]);
    subporoject_d.push(dailyAverageReportsData["totalEntriesForSubprojectD"]);
    subporoject_e.push(dailyAverageReportsData["totalEntriesForSubprojectE"]);

    totalEntries.push(dailyAverageReportsData["totalEntries"]);

    targetOfTheDay.push(dailyAverageReportsData["targetOfTheDay"]);

    utilList.push(new Utilization(dailyAverageReportsData["workingdate"], dailyAverageReportsData["dailyUtilization"]));

    utilization.push(dailyAverageReportsData["dailyUtilization"] / 100);

    absendsList.push(dailyAverageReportsData["absent"]);
    greenTargets.push(dailyAverageReportsData["targetGreen"]);
    redTargets.push(dailyAverageReportsData["targetRed"]);
    blueTargets.push(dailyAverageReportsData["targetBlue"]);
    topGreen.push(dailyAverageReportsData["targetGreen2"]);

    gammaRates.push(dailyAverageReportsData["gammarate"]);
}

// -------------------------------------------------------------------------------------------------------------------------------------------

function getDailyReports() {
	cnReport1.setMethod("GET");
	cnReport1.SetFunctionName("a");
	cnReport1.setArguments("1");
	
    var results = cnReport1.callNoXML();
    
    
    
    dailyReportsData = eval("(function(){return " + results + ";})()");

    for (var i = 0; i < dailyReportsData.length; i++) {
        dates.push(dailyReportsData[i]["workingdate"]);

        green.push(dailyReportsData[i]["green"]);
        blue.push(dailyReportsData[i]["blue"]);
        red.push(dailyReportsData[i]["red"]);

        subporoject_b.push(dailyReportsData[i]["totalEntriesForSubprojectB"]);
        subporoject_d.push(dailyReportsData[i]["totalEntriesForSubprojectD"]);
        subporoject_e.push(dailyReportsData[i]["totalEntriesForSubprojectE"]);

        totalEntries.push(dailyReportsData[i]["totalEntries"]);

        targetOfTheDay.push(dailyReportsData[i]["targetOfTheDay"]);

        utilList.push(new Utilization(dailyReportsData[i]["workingdate"], dailyReportsData[i]["dailyUtilization"]));

        utilization.push(dailyReportsData[i]["dailyUtilization"] / 100);

        absendsList.push(dailyReportsData[i]["absent"]);
        greenTargets.push(dailyReportsData[i]["targetGreen"]);
        redTargets.push(dailyReportsData[i]["targetRed"]);
        blueTargets.push(dailyReportsData[i]["targetBlue"]);
        topGreen.push(dailyReportsData[i]["targetGreen2"]);
        
        gammaRates.push(dailyReportsData[i]["gammarate"]);
    }
}

// -------------------------------------------------------------------------------------------------------------------------------------------

function getLastTargets() {
    cnTargets.SetFunctionName("getLastTargets");
    var results = cnTargets.callNoXML();
    var targetsData = eval("(function(){return " + results + ";})()");
}

// -------------------------------------------------------------------------------------------------------------------------------------------

getLastMonthAverageOfDailyReport();
getDailyReports();

// -------------------------------------------------------------------------------------------------------------------------------------------

/////////////
// Chart 1 //
/////////////

$('#container').highcharts({
    chart: {
        type: 'column'//,
        //width: 1024
    },
    title: {
        text: null
    },
    xAxis: {
        categories: dates,
        labels: {
            //formatter: function () {
            //    return '<a href="' + categoryLinks[this.value] + '">' +
            //        this.value + '</a>';
            //}
        }
    },
        
    yAxis: [{ // Primary yAxis
        labels: {
            style: {
                color: 'gray'
            }
        },
        title: {
            text: 'Χρήστες Καταχωρητικού',
            style: {
                color: 'gray'
            }
        },
        stackLabels: {
            enabled: true,
            style: {
                fontWeight: 'bold',
                color: (Highcharts.theme && Highcharts.theme.textColor) || 'gray'
            }
        }
    }, { // Secondary yAxis
        title: {
            enabled: false,
            text: 'Rate ελέγχου (%)',
            style: {
                color: '#FFA319'
            }
        },
        labels: {
            enabled: false,
            //format: '{value} %',
            style: {
                color: 'gray'
            }
        },
        opposite: true
    }, { // Third yAxis
        title: {
            enabled: false,
            text: 'Utilization (%)',
            style: {
                color: '#A3D1FF'
            }
        },
        labels: {
            //format: '{value} %',
            enabled: false,
            style: {
                color: 'gray'
            }
        },
        opposite: true
    }, { // Fourth yAxis
        title: {
            enabled: false,
            text: 'Στόχος',
            style: {
                color: '#FFA319'
            }
        },
        labels: {
            enabled: false,
            //format: '{value} %',
            style: {
                color: 'gray'
            }
        },
        opposite: true
    }, { // Fifth yAxis
        title: {
            enabled: false,
            text: 'Χαμηλότερη',
            style: {
                color: '#FFA319'
            }
        },
        labels: {
            enabled: false,
            //format: '{value} %',
            style: {
                color: 'gray'
            }
        },
        opposite: true
    }, { // sixth yAxis
        title: {
            enabled: false,
            text: 'Μεσαία',
            style: {
                color: '#FFA319'
            }
        },
        labels: {
            enabled: false,
            //format: '{value} %',
            style: {
                color: 'gray'
            }
        },
        opposite: true
    }, { // seventh yAxis
        title: {
            enabled: false,
            text: 'Ψηλότερη',
            style: {
                color: '#FFA319'
            }
        },
        labels: {
            enabled: false,
            //format: '{value} %',
            style: {
                color: 'gray'
            }
        },
        opposite: true
    }],
        
    tooltip: {
        pointFormat: '<span style="color:{series.color}">{series.name}</span>: <b>{point.y} </b><br/>',
        shared: true
    },
    plotOptions: {
        column: {
            stacking: 'normal',
            dataLabels: {
                enabled: true,
                color: (Highcharts.theme && Highcharts.theme.dataLabelsColor) || 'white',
                style: {
                    /* textShadow: '0 0 3px black, 0 0 3px black' */
                }
            }
        }
    },
    series: [{
        name: 'Πάνω από το στόχο',
        data: green /*[{y: 12, g: 45}, {y: 11, g:34}, {y:12, g:56}, {y:11, g:56}, {y:11, g:45}, {y:11, g:78}]*/,
        color: '#28b62c',
        dataLabels: {
            enabled: true,
            color: (Highcharts.theme && Highcharts.theme.dataLabelsColor) || 'white',
            style: {
                /* textShadow: '0 0 3px black, 0 0 3px black' */
            },
            formatter: function () {
                return this.y;
            }
        }
    }, {
        name: 'Έως και 20% κάτω από το στόχο',
        data: blue,
        color: '#158cba',
        dataLabels: {
            enabled: true,
            color: (Highcharts.theme && Highcharts.theme.dataLabelsColor) || 'white',
            style: {
                /* textShadow: '0 0 3px black, 0 0 3px black' */
            },
            formatter: function () {
                return this.y;
            }
        }
    }, {
        name: 'Κάτω από το στόχο',
        data: red,
        color: '#ff4136',
        dataLabels: {
            enabled: true,
            color: (Highcharts.theme && Highcharts.theme.dataLabelsColor) || 'white',
            style: {
                /* textShadow: '0 0 3px black, 0 0 3px black' */
            },
            formatter: function () {
                return this.y;
            }
        }
    },
    {
        name: 'Απόντες',
        data: absendsList,
        color: 'gray'
    },
    {
        name: 'Rate Ελέγχου',
        type: 'spline',
        yAxis: 1,
        dataLabels: {
            enabled: true,
            color: (Highcharts.theme && Highcharts.theme.dataLabelsColor) || 'white',
            formatter: function () {
                return this.y + '%';
            }
        },
        data: gammaRates,
        color: '#FFA319',
        tooltip: {
            valueSuffix: '%'
        }
    }, {
        name: 'Utilization',
        type: 'spline',
        yAxis: 1,
        color: '#003366',
        dataLabels: {
            enabled: true,
            color: (Highcharts.theme && Highcharts.theme.dataLabelsColor) || 'white',
            formatter: function () {
                return this.y + '%';
            }
        },
        data: utilization,
        tooltip: {
            valueSuffix: '%'
        }
    }, {
        name: 'Στόχος',
        type: 'spline',
        showInLegend: false,
        yAxis: 3,
        dataLabels: {
            enabled: false,
            //enabled: true,
            //x: 82,
            //y: 0,
            //verticalAlign: 'middle', // Position them vertically in the middle
            //align: 'left',
            //color: (Highcharts.theme && Highcharts.theme.dataLabelsColor) || 'gray',
            //color: (Highcharts.theme && Highcharts.theme.dataLabelsColor) || 'white',
            formatter: function () {
                return 'Στόχος:<br />' + this.y;
            }
        },
        data: greenTargets,
        //visible: false,
        color: '#28b62c',
        lineWidth: 0,
        minorGridLineWidth: 0,
        marker: {
            enabled: false,
            states: {
                hover: {
                    enabled: false
                }
            },
            fillColor: 'transparent',
            lineWidth: 0,
            lineColor: 'transparent'
        },
        minorTickLength: 0,
        tickLength: 0,
        tooltip: {
            valueSuffix: ' καταχωρήσεις'
        }
    }, {
        name: 'Χαμηλότερη',
        type: 'spline',
        showInLegend: false,
        yAxis: 4,
        dataLabels: {
            enabled: false,
            //enabled: true,
            //x: 85,
            //y: 160,
            //color: (Highcharts.theme && Highcharts.theme.dataLabelsColor) || 'gray',
            //color: (Highcharts.theme && Highcharts.theme.dataLabelsColor) || 'white',
            formatter: function () {
                return 'Χαμηλό-<br />τερη:' + this.y;
            }
        },
        data: redTargets,
        //visible: false,
        color: '#ff4136',
        lineWidth: 0,
        minorGridLineWidth: 0,
        marker: {
            enabled: false,
            states: {
                hover: {
                    enabled: false
                }
            },
            fillColor: 'transparent',
            lineWidth: 0,
            lineColor: 'transparent'
        },
        minorTickLength: 0,
        tickLength: 0,
        tooltip: {
            valueSuffix: ' καταχωρήσεις'
        }
    },{
    name: 'Μεσαία',
type: 'spline',
showInLegend: false,
yAxis: 5,
dataLabels: {
    enabled: false,
    //enabled: true,
    //x: 85,
    //y: 160,
    //color: (Highcharts.theme && Highcharts.theme.dataLabelsColor) || 'gray',
    //color: (Highcharts.theme && Highcharts.theme.dataLabelsColor) || 'white',
    formatter: function () {
        return 'Μεσαία:' + this.y;
    }
},
data: blueTargets,
//visible: false,
color: '#158cba',
lineWidth: 0,
minorGridLineWidth: 0,
marker: {
    enabled: false,
    states: {
        hover: {
                enabled: false
        }
    },
    fillColor: 'transparent',
    lineWidth: 0,
    lineColor: 'transparent'
},
minorTickLength: 0,
tickLength: 0,
tooltip: {
    valueSuffix: ' καταχωρήσεις'
}
    }, {
        name: 'Ψηλότερη',
        type: 'spline',
        showInLegend: false,
        yAxis: 6,
        dataLabels: {
            enabled: false,
            //enabled: true,
            //x: 85,
            //y: 160,
            //color: (Highcharts.theme && Highcharts.theme.dataLabelsColor) || 'gray',
            //color: (Highcharts.theme && Highcharts.theme.dataLabelsColor) || 'white',
            formatter: function () {
                return 'Ψηλότερη:' + this.y;
            }
        },
        data: /*[111, 222, 444]*/ topGreen,
        //visible: false,
        color: '#28b62c',
        lineWidth: 0,
        minorGridLineWidth: 0,
        marker: {
            enabled: false,
            states: {
                hover: {
                    enabled: false
                }
            },
            fillColor: 'transparent',
            lineWidth: 0,
            lineColor: 'transparent'
        },
        minorTickLength: 0,
        tickLength: 0,
        tooltip: {
            valueSuffix: ' καταχωρήσεις'
        }
    }]
});


// -------------------------------------------------------------------------------------------------------------------------------------------

/////////////
// Chart 2 //
/////////////

$(function () {

    $('#tplot').highcharts({
        title: {
            text: null
        },
        xAxis: {
            categories: dates
        },
        yAxis: {
            title: {
                text: 'Ολοκληρωμένες Πράξεις'
            },
            plotLines: [{
                value: 0,
                width: 1,
                color: '#313131'
            }]
        },
        tooltip: {
            valueSuffix: ' Πράξεις'
        },
        plotOptions: {
            line: {
                dataLabels: {
                    enabled: true
                },
                enableMouseTracking: true
            }
        },
        series: [{
            name: 'Υποέργο Β',
            data: subporoject_b,
            color: '#00BFFF'
            //visible: false
        }, {
            name: 'Υποέργο Δ',
            data: subporoject_d,
            color: '#FFCC00'
        }, {
            name: 'Υποέργο Ε',
            data: subporoject_e,
            color: '#00CC99'
        }, {
            name: 'Σύνολο',
            data: totalEntries,
            color: '#28b62c'
        },
           {
               name: 'Στόχος',
               data: targetOfTheDay,
               color: '#158cba',
               marker: {
                   enabled: false
               }
           }]
    });
});


// -------------------------------------------------------------------------------------------------------------------------------------------

//var templateLoader = new TemplateLoader("/reports/UtilizationTemplate.aspx");
//var utilTemplate = templateLoader.loadTemplate("templatePlaceHolder");
//
//
//var utilGrid = new Grid(utilList, document.getElementById("dailyUtilizationContainer"), utilTemplate);
//
//utilGrid.renderGrid();

// -------------------------------------------------------------------------------------------------------------------------------------------